home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / language / embedded / mcu / asembler.arc / DO5.C < prev    next >
Text File  |  1987-12-09  |  4KB  |  213 lines

  1. /*
  2.  *      MC6805 specific processing
  3.  */
  4.  
  5. /* addressing modes */
  6. #define IMMED   0       /* immediate */
  7. #define IND     1       /* indexed */
  8. #define OTHER   2       /* NOTA */
  9.  
  10. /*
  11.  *      localinit --- machine specific initialization
  12.  */
  13. localinit()
  14. {
  15. }
  16.  
  17. /*
  18.  *      do_op --- process mnemonic
  19.  *
  20.  *    Called with the base opcode and it's class. Optr points to
  21.  *    the beginning of the operand field.
  22.  */
  23. do_op(opcode,class)
  24. int opcode;    /* base opcode */
  25. int class;    /* mnemonic class */
  26. {
  27.     int     dist;   /* relative branch distance */
  28.     int     amode;  /* indicated addressing mode */
  29.     char    *peek;
  30.  
  31.     /* guess at addressing mode */
  32.     peek = Optr;
  33.     amode = OTHER;
  34.     while( !delim(*peek) && *peek != EOS)  /* check for comma in operand field */
  35.         if( *peek++ == ',' ){
  36.             amode = IND;
  37.             break;
  38.             }
  39.     if( *Optr == '#' ) amode = IMMED;
  40.  
  41.     switch(class){
  42.         case INH:                       /* inherent addressing */
  43.             emit(opcode);
  44.             return;
  45.         case GEN:                       /* general addressing */
  46.             do_gen(opcode,amode);
  47.             return;
  48.         case REL:                       /* short relative branches */
  49.             eval();
  50.             dist = Result - (Pc+2);
  51.             emit(opcode);
  52.             if( (dist >127 || dist <-128) && Pass==2){
  53.                 error("Branch out of Range");
  54.                 emit(lobyte(-2));
  55.                 return;
  56.                 }
  57.             emit(lobyte(dist));
  58.             return;
  59.         case NOIMM:
  60.             if( amode == IMMED ){
  61.                 error("Immediate Addressing Illegal");
  62.                 return;
  63.                 }
  64.             do_gen(opcode,amode);
  65.             return;
  66.         case GRP2:
  67.             if( amode == IND ){
  68.                 do_indexed(opcode+0x20);
  69.                 return;
  70.                 }
  71.             eval();
  72.             Cycles += 2;
  73.             if(Force_byte){
  74.                 emit(opcode);
  75.                 emit(lobyte(Result));
  76.                 return;
  77.                 }
  78.             if(Result>=0 && Result <=0xFF){
  79.                 emit(opcode);
  80.                 emit(lobyte(Result));
  81.                 return;
  82.                 }
  83.             error("Extended Addressing not allowed");
  84.             return;
  85.         case SETCLR:
  86.         case BTB:
  87.             eval();
  88.             if(Result <0 || Result >7){
  89.                 error("Bit Number must be 0-7");
  90.                 return;
  91.                 }
  92.             emit( opcode | (Result << 1));
  93.             if(*Optr++ != ',')error("SYNTAX");
  94.             eval();
  95.             emit(lobyte(Result));
  96.             if(class==SETCLR)
  97.                 return;
  98.             /* else it's bit test and branch */
  99.             if(*Optr++ != ',')error("SYNTAX");
  100.             eval();
  101.             dist = Result - (Old_pc+3);
  102.             if( (dist >127 || dist <-128) && Pass==2){
  103.                 error("Branch out of Range");
  104.                 emit(lobyte(-3));
  105.                 return;
  106.                 }
  107.             emit(lobyte(dist));
  108.             return;
  109.         default:
  110.             fatal("Error in Mnemonic table");
  111.         }
  112. }
  113.  
  114. /*
  115.  *      do_gen --- process general addressing
  116.  */
  117. do_gen(op,mode)
  118. int     op;
  119. int     mode;
  120. {
  121.     if( mode == IMMED){
  122.         Optr++;
  123.         emit(op);
  124.         eval();
  125.         emit(lobyte(Result));
  126.         return;
  127.         }
  128.     else if( mode == IND ){
  129.         do_indexed(op+0x30);
  130.         return;
  131.         }
  132.     else if( mode == OTHER){        /* direct or extended addressing */
  133.         eval();
  134.         if(Force_word){
  135.             emit(op+0x20);
  136.             eword(Result);
  137.             Cycles += 3;
  138.             return;
  139.             }
  140.         if(Force_byte){
  141.             emit(op+0x10);
  142.             emit(lobyte(Result));
  143.             Cycles += 2;
  144.             return;
  145.             }
  146.         if(Result >= 0 && Result <= 0xFF){
  147.             emit(op+0x10);
  148.             emit(lobyte(Result));
  149.             Cycles += 2;
  150.             return;
  151.             }
  152.         else {
  153.             emit(op+0x20);
  154.             eword(Result);
  155.             Cycles += 3;
  156.             return;
  157.             }
  158.         }
  159.     else {
  160.         error("Unknown Addressing Mode");
  161.         return;
  162.         }
  163. }
  164.  
  165. /*
  166.  *      do_indexed --- handle all wierd stuff for indexed addressing
  167.  */
  168. do_indexed(op)
  169. int op;
  170. {
  171.     eval();
  172.     if(!(*Optr++ == ',' && (*Optr == 'x' || *Optr == 'X')))
  173.         warn("Indexed Addressing Assumed");
  174.     if(Force_word){
  175.         if(op < 0x80 ){ /* group 2, no extended addressing */
  176.             emit(op+0x10); /* default to one byte indexed */
  177.             emit(lobyte(Result));
  178.             Cycles += 3;
  179.             return;
  180.             }
  181.         emit(op);
  182.         eword(Result);
  183.         Cycles += 4;
  184.         return;
  185.         }
  186.     Cycles += 3;    /* assume 1 byte indexing */
  187.     if(Force_byte){
  188.         emit(op+0x10);
  189.         emit(lobyte(Result));
  190.         return;
  191.         }
  192.     if(Result==0){
  193.         emit(op+0x20);
  194.         Cycles--;       /* ,x slightly faster */
  195.         return;
  196.         }
  197.     if(Result>0 && Result <=0xFF){
  198.         emit(op+0x10);
  199.         emit(lobyte(Result));
  200.         return;
  201.         }
  202.     if( op < 0x80 ){
  203.         warn("Value Truncated");
  204.         emit(op+0x10);
  205.         emit(lobyte(Result));
  206.         return;
  207.         }
  208.     emit(op);
  209.     eword(Result);
  210.     Cycles++;       /* 2 byte slightly slower */
  211.     return;
  212. }
  213.